﻿function chkCompany() {
    if ($("#drp_buyer").val() == "") {
        $("#val_company").show();
        return false;
    }
    if ($("#ext_startTime").val() == "") {
        $("#val_startTime").show();
        return false;
    }
    if ($("#ext_endTime").val() == "") {
        $("#val_endTime").show();
        return false;
    }
    var isNext = true;
    if ($("#ext_startTime").val() != "" && $("#ext_endTime").val() != "") {
        $.ajax({
            url: 'buyerFixedPricePeriod.ashx',
            type: 'GET',
            dataType: "json",
            async: false,
            data: { Param: "ifExist", buyerid: $("#drp_buyer").val(), startTime: $("#ext_startTime").val(), endTime: $("#ext_endTime").val() },
            cache: false,
            success: function(json) {
                if (json.length > 0) {
                    $(json).each(function(entryIndex, entry) {
                        alert("对不起，您填写的配送周期有误！此单位上次配送周期为" + entry["startTime"] + "至" + entry["endTime"]);
                        isNext = false;
                    })
                }
            }
        });
    }
    if (!isNext)
        return false;
}
function checkProducts() {
    chkCompany();
    if ($("#hd_proScope").val() == "") {
        $("#val_proScope").show();
        return false;
    }
    return true;
}
function checkPrice(obj) {
    if (obj.value != "" && obj.value != "0") {
        var flag = IsPriceNumeric(obj.value);
        if (!flag)
            obj.value = "1";
    }
    else
        obj.value = "1";
}
function IsPriceNumeric(oNum) {
    var strP = /^[0-9]+([.]\d{1,2})?$/;
    if (strP.test(oNum))
        return true;
    else
        return false;
}
function resetSrh() {
    document.getElementById("txt_ProductNo").value = "";
    document.getElementById("txt_ProductName").value = "";
}
function addTobfpItem(obj, productId, unit) {
    if ($('#div_AddProduct').style = "display:none") {
        $("#div_AddProduct").css("display", "block");
    }

    var products = [];
    if ($('#hd_proScope').val().length > 0) {
        products = jsonParse($('#hd_proScope').val());
    }
    var isExists = false;

    $(products).each(function(entryIndex, entry) {
        if (entry['productId'] == productId) {
            isExists = true;
            return false;
        }
    });

    var productName = $.trim($(obj).parent().prev().prev().prev().prev().text());
    var sellerPrice = $.trim($($(obj).parent().prev().prev().prev().find('input')[0]).val());
    var price = $($(obj).parent().prev().prev().find('input')[0]).val();
    var sellerId = $($(obj).parent().prev().find('select')[0]).val();
    var sellerName = $($(obj).parent().prev().find('option:selected')[0]).text();
    if (!isExists) {
        var product = { 'productId': productId, 'unit': unit, 'productName': productName, 'price': price, 'sellerId': sellerId, 'sellerName': sellerName, 'sellerPrice': sellerPrice };
        products.push(product);
        $('#hd_proScope').val(Object.serialize2Str(products));
        updateTableRow();
    }
    else {
        alert('您选择的商品已经存在！');
        return false;
    }
}
function updateTableRow() {

    var products = [];
    if ($('#hd_proScope').val().length > 0) {
        products = jsonParse($('#hd_proScope').val());
        var sw = [];
        for (var i = 0; i < products.length; i++) {
            sw.push('<tr align="center" style="height: 20px;">');
            sw.push('<td bgcolor="#FFFFFF">' + (i + 1) + '</td>');
            sw.push('<td bgcolor="#FFFFFF"><a target="_blank" href="../user_productDetail_' + products[i]['productId'] + '.html">' + products[i]['productName'] + '</a></td>');
            sw.push('<td bgcolor="#FFFFFF"><span class="red">' + products[i]['sellerPrice'] + '</span></td>');
            sw.push('<td bgcolor="#FFFFFF"><span class="red">' + products[i]['price'] + '</span></td>');
            sw.push('<td bgcolor="#FFFFFF">' + products[i]['sellerName'] + '</td>');
            sw.push('<td bgcolor="#FFFFFF"><a href="javascript:void(0);" onclick="delFromHiddenValue(' + products[i]['productId'] + ');">删除</a></td>');
            sw.push('</tr>');
        }
        $('#tableBody').empty();
        $('#tableBody').append(sw.join(''));
    }
}
function delFromHiddenValue(productId) {
    var products = [];
    if ($('#hd_proScope').val().length > 0) {
        products = jsonParse($('#hd_proScope').val());
        for (var i = 0; i < products.length; i++) {
            if (products[i]["productId"] == productId) {
                products.remove(i);
                break;
            }
        }
        $('#hd_proScope').val(Object.serialize2Str(products));
        updateTableRow();
    }
}